import {auditRegisterGet,auditRegisterOperate,auditRegisterBatchOperate,getTeacherInfo} from "../../api/auditRegister";

const state = {
    //注册信息
    auditRegisterInfo:[],
    //信息总数
    total:0,
    //老师详细信息
    teacherInfo:{
        account:'',
        password:'',
        name:'',
        e_mail:'',
        college:'',
        phone:''
    }
}

const mutations = {
    // mutations改变state中的数据
    SET_AUD_REG_INFO: (state, info) => { // 存储token
        const array=[];
        for(let i of info){
            //返回数据做进一步处理
            array.push({
                key: i.auditRegisterId,
                content: '工号为 '+i.teaNumber+"的 "+i.teacherName+"老师 申请了一个老师账号",
                result: i.result===1 ? '未审核':(i.result===2 ? "已通过": "已驳回"),
                teacherId: i.teacherId,
            })
        }
        state.auditRegisterInfo=array;
    },
    SET_TOTAL: (state,total) => {
        state.total=total;
    },
    //空方法
    NULL: () => {},
    SET_TEACHER_INFO: (state,info)=>{
        state.teacherInfo.account=info.simpleTeacherInfo.account;
        state.teacherInfo.password=info.simpleTeacherInfo.password;
        state.teacherInfo.name=info.simpleTeacherInfo.name;
        state.teacherInfo.college=info.simpleTeacherInfo.college;
        state.teacherInfo.e_mail=info.email;
        state.teacherInfo.phone=info.phone;
    },
}

const actions = {
    // 发送请求
    // commit 是调用上面mutations中函数的，state是上面的state
    //调用引入的api方法，还用处理数据

    auditRegisterGet({ commit },audRegGetCond){
        return new Promise((resolve, reject) => {
            auditRegisterGet(audRegGetCond).then( data => {
                //console.log(data)
                if(data.code === 1){ // 获取成功
                    commit('SET_AUD_REG_INFO', data.data.data);
                    commit('SET_TOTAL',data.data.data.total);
                    //console.log("调用auditRegisterGet成功", data);
                    resolve(data);
                }else{
                    reject(data.msg);
                }
            } ).catch(error=>{
                reject(error)
            })
        })
    },
    auditRegisterOperate({commit},audRegInfo){
        commit('NULL');
        return new Promise((resolve, reject) => {
            auditRegisterOperate(audRegInfo).then(data => {
                //console.log(data)
                if(data.code === 1){ // 获取成功
                    //console.log("调用auditRegisterOperate成功", data);
                    resolve(data);
                }else{
                    reject(data.msg)
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
    auditRegisterBatchOperate({commit},audRegInfo){
        commit('NULL');
        return new Promise((resolve, reject) => {
            auditRegisterBatchOperate(audRegInfo).then(data => {
                //console.log(data)
                if(data.code === 1){ // 获取成功
                    //console.log("调用auditRegisterBatchOperate成功", data);
                    resolve(data);
                }else{
                    reject(data.msg);
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },
    getTeacherInfo({commit},params){
        return new Promise((resolve, reject) => {
            getTeacherInfo(params).then(data => {
                //console.log(data)
                if(data.code === 1){ // 获取成功
                    //console.log("getTeacherInfo调用成功", data);
                    commit("SET_TEACHER_INFO",data.data);
                    resolve(data);
                }else{
                    reject(data.msg);
                }
            }).catch(error=>{
                reject(error)
            })
        })
    }
}

export default {
    //为true，则可以模块化引用
    namespaced: true,
    state,
    mutations,
    actions
}
